package com.ysstech.reportworld.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.DataDemandBug;
import com.ysstech.reportworld.service.DataDemandBugService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * 报表世界数据和需求BUG关联关系表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-12-27
 */
@Controller
@Slf4j
@Api(tags = "报表世界数据和需求BUG关联关系管理api")
@RequestMapping("/dataDemandBug")
public class DataDemandBugController extends BaseController {

    @Autowired
    private DataDemandBugService dataDemandBugService;

    /**
     * 新增需求BUG关联关系
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "新增需求BUG关联关系")
    @YssLog(module = "需求BUG关联关系管理", remark = "新增需求BUG关联关系")
    @PostMapping(value = "/addDataDemandBug")
    @ResponseBody
    public RestResult addDataDemandBug(DataDemandBug dataDemandBug) throws Exception {
        if (null == dataDemandBug || StringUtils.isBlank(dataDemandBug.getDemandBugId())
                || StringUtils.isBlank(dataDemandBug.getDataId()) || StringUtils.isBlank(dataDemandBug.getDataCode())) {
            throw new BusinessException("新增需求BUG关联关系失败，请稍后重试！");
        }
        dataDemandBug.setId(DateUtil.generateUUID());
        dataDemandBug.setAddTime(LocalDateTime.now());
        dataDemandBug.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        return ResultGenerator.getSuccessResult("新增需求BUG关联关系成功",
                dataDemandBugService.save(dataDemandBug));

    }

    /**
     * 修改需求BUG关联关系
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "修改需求BUG关联关系")
    @YssLog(module = "需求BUG关联关系管理", remark = "修改需求BUG关联关系")
    @PostMapping(value = "/editDataDemandBug")
    @ResponseBody
    public RestResult editDataDemandBug(DataDemandBug dataDemandBug) throws Exception {
        if (null == dataDemandBug || StringUtils.isBlank(dataDemandBug.getId())) {
            throw new BusinessException("修改需求BUG关联关系失败，请稍后重试！");
        }
        dataDemandBug.setEditTime(LocalDateTime.now());
        dataDemandBug.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        return ResultGenerator.getSuccessResult("修改需求BUG关联关系成功",
                dataDemandBugService.updateById(dataDemandBug));
    }


    /**
     * 删除需求BUG关联关系
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "删除需求BUG关联关系")
    @YssLog(module = "需求BUG关联关系管理", remark = "删除需求BUG关联关系")
    @PostMapping(value = "/delDataDemandBug")
    @ResponseBody
    public RestResult delDataDemandBug(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("删除需求BUG关联关系失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        // 查询删除的数据的code
        QueryWrapper<DataDemandBug> wrapper = new QueryWrapper<>();
        wrapper.select("DATA_CODE");
        wrapper.groupBy("DATA_CODE");
        List<DataDemandBug> stringList = dataDemandBugService.list(wrapper);
        if (!CollectionUtils.isEmpty(stringList)) {
            for (DataDemandBug demandBug : stringList) {
                if (null == demandBug || StringUtils.isBlank(demandBug.getDataCode())) {
                    continue;
                }
            }
        }
        return ResultGenerator.getSuccessResult("删除需求BUG关联关系成功",
                dataDemandBugService.removeByIds(list));
    }


    /**
     * 检查是不是已经存在该需求BUG关联关系
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "检查需求BUG关联关系")
    @YssLog(module = "需求BUG关联关系管理", remark = "检查需求BUG关联关系")
    @PostMapping(value = "/dataDemandBugCheck")
    @ResponseBody
    public RestResult dataDemandBugCheck(DataDemandBug dataDemandBug) throws Exception {
        if (null == dataDemandBug || StringUtils.isBlank(dataDemandBug.getDemandBugId())
                || StringUtils.isBlank(dataDemandBug.getDataId()) || StringUtils.isBlank(dataDemandBug.getDataCode())) {
            throw new BusinessException("检查需求BUG关联关系失败，请稍后重试！");
        }
        QueryWrapper<DataDemandBug> wrapper = new QueryWrapper<>();
        wrapper.eq("DEMAND_BUG_ID", dataDemandBug.getDemandBugId());
        wrapper.eq("DATA_ID", dataDemandBug.getDataId());
        wrapper.eq("DATA_CODE", dataDemandBug.getDataCode());
        int count = dataDemandBugService.count(wrapper);
        //查询是否存在code值得参数
        if (count > 0) {//返回true表示已经存在
            return ResultGenerator.getSuccessResult("检查需求BUG关联关系成功", false);
        } else {
            return ResultGenerator.getSuccessResult("检查需求BUG关联关系成功", true);
        }
    }
}
